package sample;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sample.model.Company;

import java.io.IOException;

public class MainApp extends Application {

    public Stage primaryStage;
    private BorderPane rootLayout;
    private static final Logger logger = LoggerFactory.getLogger(MainApp.class);

    ObservableList<Company> personData = FXCollections.observableArrayList();

    Stage stage = new Stage();

    public void showMainWin() throws Exception {
        this.start(stage);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        // FIXME for test
        logger.info("enter app:{}", "test");

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("EMapCrawler");

        initRootLayout();

        showPersonOverview();

    }

    private void initRootLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void showPersonOverview() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/EMapMain.fxml"));
            AnchorPane qrOverview = (AnchorPane) loader.load();

            rootLayout.setCenter(qrOverview);

            EMapMainController controller = loader.getController();
            controller.setMain(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        launch(args);
    }

    public MainApp() {
    }

    public ObservableList<Company> getQrData() {
        return personData;
    }
}
